package com.adgox.tiantianbiting.redpacket.sign;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.SignAdapter;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.SignCheckTodayBean;
import com.adgox.tiantianbiting.bean.SignWeekBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.mine.tradeRecord.TradeRecordActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignActivity extends AppCompatActivity {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    private int userId;
    private double rewardBalance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        ButterKnife.bind(this);
        UserInfo user = SessionManager.getDefault().getUser();
        userId = user.getId();
        rewardBalance = user.getRewardBalance();
        //检测今天是否已经签到
        checkToday();

        init();
    }

    private void checkToday() {
        OkGo.<BaseResponseBean<SignCheckTodayBean>>get(ApiConstant.BASE_URL + ApiConstant.CHECK_TODAY_SIGN)
                .params("userId", userId)
                .execute(new JsonCallback<BaseResponseBean<SignCheckTodayBean>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<SignCheckTodayBean>> response) {
                        if ("0".equals(response.body().getCode()) && response.body().getData() != null){
                            SignCheckTodayBean bean = response.body().getData();
                            if (bean.getCurrentDaySignIn() == 0){
                                getSignData();
                            }
                        }
                    }
                });
    }

    private void getSignData() {
        OkGo.<BaseResponseBean<List<SignWeekBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_SIGN_LIST)
                .params("userId", userId)
                .params("flg", 1)
                .execute(new JsonCallback<BaseResponseBean<List<SignWeekBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<SignWeekBean>>> response) {
                        if ("0".equals(response.body().getCode())&&response.body().getData()!=null) {
                            SignWeekBean signWeekBean = response.body().getData().get(0);
                            DialogUtils.showSignDialog(SignActivity.this,signWeekBean);
                        }
                    }
                });
    }

    private void init() {

        tvAmount.setText(rewardBalance + "");

        OkGo.<BaseResponseBean<List<SignWeekBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_SIGN_LIST)
                .params("userId", userId)
                .params("flg", 1)
                .execute(new JsonCallback<BaseResponseBean<List<SignWeekBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<SignWeekBean>>> response) {
                        if (!"0".equals(response.body().getCode())) {
                            Toast.makeText(SignActivity.this, "获取签到信息失败", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        LinearLayoutManager lm = new LinearLayoutManager(SignActivity.this);
                        rv.setLayoutManager(lm);
                        SignAdapter signAdapter = new SignAdapter(response.body().getData());
                        rv.setAdapter(signAdapter);
                    }
                });
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    @OnClick(R.id.tv_check)
    public void onCheckClicked() {
        startActivity(new Intent(this, TradeRecordActivity.class));
    }
}
